package com.tidc.seaquartz;

import com.tidc.seaservermapper.dao.RabbitMessageLogDao;
import com.tidc.seaservermodel.entity.RabbitMessageLog;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
@MapperScan(basePackages = "com.tidc.seaservermapper")
@SpringBootApplication
@ComponentScan(value="com.tidc")
@Slf4j
public class SeaQuartzApplication {
    @Resource
    private CachingConnectionFactory cachingConnectionFactory;
    @Resource
    private RabbitMessageLogDao rabbitMessageLogDao;
    public static void main(String[] args) {
        SpringApplication.run(SeaQuartzApplication.class, args);
    }
//  mysql 多数据源
    @Bean
    RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
//		设置消息发送的回调
        rabbitTemplate.setConfirmCallback((data, ack, cause) -> {
            if (ack) {
                RabbitMessageLog rabbitMessageLog = rabbitMessageLogDao.queryByMsgId(data.getId());
                log.info(rabbitMessageLog + ":消息发送成功");
                rabbitMessageLogDao.updateLogStatus(1,rabbitMessageLog.getMsgId());
            } else {
                log.info(data.getId() + ":消息发送失败");

            }
        });
//		消息发送失败: 1.直接失败 2.消息从交换机发送到队列失败
        rabbitTemplate.setReturnCallback((msg, repCode, repText, exchange, routingkey) -> {
            log.info("消息发送失败");
        });
        return rabbitTemplate;
    }
}
